//
//  HomeViewController.m
//  忘不了
//
//  Created by rimi on 16/7/7.
//  Copyright © 2016年 handuan. All rights reserved.
//

#import "HomeViewController.h"
#import "BaseRequest.h"
#import "SDAutoLayout.h"
#import "BuyViewController.h"
#import "MoneyViewController.h"
#import "LoginViewController.h"
#import "AppDelegate.h"
#import "RemindMeViewController.h"
#import "leftViewController.h"
#import "selfInfoViewController.h"
#import "descriptionViewController.h"
#import "questionViewController.h"
#import "adviceViewController.h"
#import "UserModel.h"
#import "BaseRequest.h"
#import "MJRefresh.h"
#import "CheckWeb.h"
#import "UIViewController+MMDrawerController.h"//控制能否打开抽屉


// 取得AppDelegate单例
#define ShareApp ((AppDelegate *)[[UIApplication sharedApplication] delegate])

@interface HomeViewController ()<leftViewControllerDelegate>
@property (nonatomic,strong)UIButton *menuBUtton;
@property (nonatomic,assign)BOOL isSelected;
@property (nonatomic,strong)UIImageView *noticeImage;
@property (nonatomic,strong)UILabel *noticeTitleLabel;
@property (nonatomic,strong)UILabel *noticeMessageLabel;
@end
@implementation HomeViewController

//页面将出现，可以打开抽屉
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    [self.mm_drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    }

//页面将消失，不能打开抽屉
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
    [self.mm_drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeNone];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [NSThread sleepForTimeInterval:0.5];
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"个人"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonTaped)];
    self.navigationItem.leftBarButtonItem = item;
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.0256 green:0.1514 blue:0.3238 alpha:1.0];
    //隐藏导航栏返回按钮自带的文字
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
    //导航栏前景色(只能改变导航栏上面的按钮颜色等)
    self.navigationController.navigationBar.tintColor = [UIColor brownColor];
    //导航栏背景颜色
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.9216 green:0.8902 blue:0.8392 alpha:1.0];
    //设置导航栏标题颜色、字体大小等
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor brownColor]}];
    [self loadUI];
    NSDictionary *userInfo = [[UserModel shareUserModel] readUserNameAndPassword];
    if (userInfo != nil) {
        [self autoLogin];
    } else {
        return;
    }
    
}

//自动登录
- (void)autoLogin {
    NSDictionary *userInfo = [[UserModel shareUserModel] readUserNameAndPassword];
    [BaseRequest startLoginWithUserName:userInfo[@"username"] password:userInfo[@"password"] completionhanlder:^(Response *response) {
        if (response.isSuccess) {
            [UserModel shareUserModel].isLogin = YES;
        }
    }];
}

- (void)loadUI {
    UIImageView *backGroundImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"s"]];
    backGroundImage.frame = self.view.frame;
    [self.view addSubview:backGroundImage];
    NSArray *backImageArr = @[@"出门买什么",@"钱都去哪了",@"记得提醒我"];
    for (NSInteger index = 0; index < 3; index ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.bounds = CGRectMake( 0,0, self.view.bounds.size.width * 0.7, self.view.bounds.size.height * 0.1);
        if (index % 2 == 0) {
            button.center = CGPointMake(- 800, self.view.bounds.size.height * 0.3 + index *self.view.bounds.size.height * 0.15);
        }else{
            button.center = CGPointMake(800, self.view.bounds.size.height * 0.3 + index *self.view.bounds.size.height * 0.15);
        }
        button.tag = index + 1000;
        [button setBackgroundImage:[UIImage imageNamed:backImageArr[index]] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(menu:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
    }
    self.noticeImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"首页公告"]];
    self.noticeImage.bounds = CGRectMake(0, -1000, self.view.bounds.size.width * 0.8, self.view.bounds.size.height * 0.5);
    self.noticeImage.center = CGPointMake(self.view.center.x, - 1000);
    [self.view addSubview:self.noticeImage];
    
       _menuBUtton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_menuBUtton setTitleColor:[UIColor colorWithRed:0.5925 green:0.1289 blue:0.2467 alpha:1.0] forState:UIControlStateNormal];
    [_menuBUtton setTitle:@"功能选择" forState:UIControlStateNormal];
    [_menuBUtton setBackgroundImage:[UIImage imageNamed:@"首页按钮"] forState:UIControlStateNormal];
    [_menuBUtton addTarget:self action:@selector(openMenu:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_menuBUtton];
    _menuBUtton.sd_layout
    .centerXIs(self.view.frame.size.width/2)
    .centerYIs(self.view.frame.size.height * 0.8)
    .widthRatioToView(self.view,0.3).heightEqualToWidth();
    
    _noticeTitleLabel = [[UILabel alloc]init];
    _noticeTitleLabel.bounds = CGRectMake(0, 0,self.view.frame.size.width * 0.5,self.view.frame.size.width * 0.1);
    _noticeTitleLabel.center = CGPointMake(self.view.center.x, -1000);
    _noticeTitleLabel.text = @"生活小贴士";
    _noticeTitleLabel.backgroundColor = [UIColor clearColor];
    _noticeTitleLabel.textColor = [UIColor colorWithRed:0.5686 green:0.1451 blue:0.2588 alpha:1.0];
    _noticeTitleLabel.textAlignment = NSTextAlignmentCenter;
    _noticeTitleLabel.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:_noticeTitleLabel];
    
    [self noticeShow];
    
}
- (void)noticeShow{
    
    [UIView animateWithDuration:0.5 animations:^{
        self.noticeImage.center = CGPointMake(self.view.center.x, self.view.center.y);
        self.noticeTitleLabel.center = CGPointMake(self.view.center.x, self.view.center.y-20);
    } completion:^(BOOL finished) {
        _noticeMessageLabel = [[UILabel alloc]init];
        _noticeMessageLabel.bounds = CGRectMake(0, 0, self.view.frame.size.width * 0.5, self.view.frame.size.width * 0.3);
        _noticeMessageLabel.center = CGPointMake(self.view.center.x, self.view.center.y+40);
        _noticeMessageLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
        _noticeMessageLabel.font = [UIFont systemFontOfSize:18];
        _noticeMessageLabel.textAlignment = NSTextAlignmentLeft;
        _noticeMessageLabel.font = [UIFont systemFontOfSize:18];
        _noticeMessageLabel.numberOfLines = 0;
        [self.view addSubview:_noticeMessageLabel];
        BOOL isWeb = [CheckWeb checkWang];
        if (isWeb == NO) {
            _noticeMessageLabel.textAlignment = NSTextAlignmentCenter;
            _noticeMessageLabel.text =  @"请检查网络";
        }else{
            [BaseRequest startGetLifeKeyWithcompletionhanlder:^(Response *response) {
                if (response.isSuccess) {
                    NSArray *noticeArray = response.resultObject;
                    int x = arc4random() % (noticeArray.count - 1);
                    _noticeMessageLabel.text =  noticeArray[x][@"key"];
                }
            }];
        }
       
    } ];
    
}
//实现代理方法
- (void)getText:(NSString *)text {
    if ([text isEqual:@"头像"]) {
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        [self.navigationController pushViewController:loginVC animated:YES];
    }
    if ([text isEqual:@"未登录"]) {
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        [self.navigationController pushViewController:loginVC animated:YES];
    }
    if ([text isEqual:@"个人信息"]) {
        selfInfoViewController *selfVC = [[selfInfoViewController alloc]init];
        [self.navigationController pushViewController:selfVC animated:YES];
    }
    if ([text isEqual:@"软件简介"]) {
        descriptionViewController *descVC = [[descriptionViewController alloc]init];
        [self.navigationController pushViewController:descVC animated:YES];
    }
    if ([text isEqual:@"常见问题"]) {
        questionViewController *questionVC = [[questionViewController alloc]init];
        [self.navigationController pushViewController:questionVC animated:YES];
    }
    if ([text isEqual:@"意见反馈"]) {
        adviceViewController *adviceVC = [[adviceViewController alloc]init];
        [self.navigationController pushViewController:adviceVC animated:YES];
    }
    //关闭抽屉
    [ShareApp.drawerController closeDrawerAnimated:YES completion:nil];
    
}
- (void)buttonTaped {
    //打开抽屉
    [ShareApp.drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}
- (void)openMenu:(UIButton *)sender{
    sender.selected = !sender.selected;
    sender.userInteractionEnabled = NO;
    if (sender.selected) {
        [UIView animateWithDuration:0.2 animations:^{
            self.noticeImage.center = CGPointMake(self.view.center.x, - 1000);
            self.noticeTitleLabel.center = CGPointMake(self.view.center.x, - 1000);
            self.noticeMessageLabel.center = CGPointMake(self.view.center.x, - 1000);
        } completion:^(BOOL finished) {
            UIButton *buyButton = [self.view viewWithTag:1000];
            UIButton *rememberButton = [self.view viewWithTag:1001];
            UIButton *moneyButton = [self.view viewWithTag:1002];
            [UIView animateWithDuration:0.2 animations:^{
                 buyButton.center = CGPointMake(self.view.center.x, self.view.bounds.size.height * 0.3 + 0 * self.view.bounds.size.height * 0.15);
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.2 animations:^{
                    rememberButton.center = CGPointMake(self.view.center.x, self.view.bounds.size.height * 0.3 + 1 * self.view.bounds.size.height * 0.15);
                }completion:^(BOOL finished) {
                    [UIView animateWithDuration:0.2 animations:^{
                     moneyButton.center = CGPointMake(self.view.center.x, self.view.bounds.size.height * 0.3 + 2 * self.view.bounds.size.height * 0.15);
                         sender.userInteractionEnabled = YES;
                    }];
                   
                }];
            }];
        }];
    }else{
        UIButton *buyButton = [self.view viewWithTag:1000];
        UIButton *rememberButton = [self.view viewWithTag:1001];
        UIButton *moneyButton = [self.view viewWithTag:1002];
        [UIView animateWithDuration:0.2 animations:^{
            buyButton.center = CGPointMake(- 800, self.view.bounds.size.height * 0.3 + 0 * self.view.bounds.size.height * 0.15);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.2 animations:^{
                rememberButton.center = CGPointMake(800, self.view.bounds.size.height * 0.3 + 1 * self.view.bounds.size.height * 0.15);
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.2 animations:^{
                    moneyButton.center = CGPointMake(-800, self.view.bounds.size.height * 0.3 + 2 * self.view.bounds.size.height * 0.15);
                }completion:^(BOOL finished) {
                    [UIView animateWithDuration:0.2 animations:^{
                    self.noticeImage.center = CGPointMake(self.view.center.x, self.view.center.y);
                    self.noticeTitleLabel.center = CGPointMake(self.view.center.x, self.view.center.y-20);
                    self.noticeMessageLabel.center = CGPointMake(self.view.center.x, self.view.center.y+40);
                    }];
                    sender.userInteractionEnabled = YES;
                }];
            }];
        }];
    }
}
- (void)menu:(UIButton *)sender{
    switch (sender.tag) {
        case 1000:{
            BuyViewController *buy = [[BuyViewController alloc]init];
            [self.navigationController pushViewController:buy animated:YES];}
            break;
        case 1001:{
            if ([UserModel shareUserModel].isLogin == YES) {
                MoneyViewController *moneyVC = [[MoneyViewController alloc]init];
                [self.navigationController pushViewController:moneyVC animated:YES];
            } else {
                UIAlertController *tips = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请先登录" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
                [tips addAction:sure];
                [self presentViewController:tips animated:YES completion:nil];
            }
        }
            break;
        case 1002:{
            RemindMeViewController *reminVC = [RemindMeViewController getVC];
            [self.navigationController pushViewController:reminVC animated:YES];}
            break;
        default:
            break;
    }
}
@end

